package com.t2t.top.arr;

import java.util.HashMap;
import java.util.Map;

/**
 * HashMap并发测试----两个线程对同一位置同时访问，造成数据丢失
 * 1、数量少
 * 2、死循环
 * 3、扩容时出现问题
 *
 * @author huangsheng
 */
public class HashMapTestInThread {

	public static void main(String args[]) {
		for (int i = 0; i < 10; i++) {
			Map dataMap = new HashMap<String, String>();
			Thread t1 = new Thread(new AddThread(0, dataMap));
			Thread t2 = new Thread(new AddThread(1, dataMap));
			t1.start();
			t2.start();
			try {
				t1.join();
				t2.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				System.out.println("join error" + e.getStackTrace());
			}
			System.out.println(dataMap.size());
			System.out.println(dataMap);
			//System.out.println(dataMap.keySet().size());
			System.out.println("==========================================");

		}
	}

}

class AddThread implements Runnable {

	int start = 0;
	Map dataMap = null;

	public AddThread(int start, Map dataMap) {
		this.start = start;
		this.dataMap = dataMap;
	}

	@Override
	public void run() {
		for (int i = start; i < 1000; i += 2) {
			dataMap.put(String.valueOf(i), String.valueOf(i));
		}
	}
}
